/*******************************************************************************
 * FilePath    : /Src/BSP/RTC.c
 * Version     : 1.0
 * Brief       : 
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2020-04-03 15:06:14
 * LastEditors : AndrewHu
 * LastEditTime: 2021-05-21 11:09:34
 * Description : 
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
RTC_HandleTypeDef RTC_InitStructure = {0};
RTC_TimeTypeDef RTC_TimeCurrent = {0};
RTC_DateTypeDef RTC_DateCurrent = {0};

/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/
/*******************************************************************************
 * Function    : RTC_Init
 * Brief       : RTC初始化
 * param        {*}
 * return       {*}
 * Description : 建议初始化一次，如果RTC时钟有改变，需要重新上电
 * Date        : 2021-05-21 11:06:45
 ******************************************************************************/
void RTC_Init(void)
{
    __HAL_RCC_RTC_CLK_ENABLE();

    RTC_InitStructure.Instance = RTC;
    RTC_InitStructure.Init.ClockSource = SET_RTC_CLOCK;
    RTC_InitStructure.Init.HourFormat = RTC_HOURFORMAT_24;         //RTC_HOURFORMAT_12;
    RTC_InitStructure.Init.TimeAdjustMode = RTC_TIME_ADJUST_SEC30; //多久校准一次
    RTC_InitStructure.Init.TimeTrim = 0x00;                        //校准参数，屏蔽RTC时钟周期数
    RTC_InitStructure.State = HAL_RTC_STATE_RESET;
    HAL_RTC_Init(&RTC_InitStructure);
}
/*******************************************************************************
 * Function    : Calendar_Init
 * Brief       : 万年历初始化
 * param        {*}
 * return       {*}
 * Description : 
 * Date        : 2021-05-21 10:49:41
 ******************************************************************************/
void Calendar_Init(void)
{
    RTC_TimeTypeDef RTC_TimeStructure = {0};
    RTC_DateTypeDef RTC_DateStructure = {0};

    /*set time and date */
    RTC_TimeStructure.Hours = 12;
    RTC_TimeStructure.Minutes = 30;
    RTC_TimeStructure.Seconds = 00;
    RTC_DateStructure.Date = 1;
    RTC_DateStructure.Month = 1;
    RTC_DateStructure.Year = 2021;
    RTC_DateStructure.WeekDay = RTC_WEEKDAY_FRIDAY;
    //	HAL_RTC_SetTime(&RTC_InitStructure, &RTC_TimeStructure, RTC_InitStructure.Init.HourFormat);
    //	HAL_RTC_SetDate(&RTC_InitStructure, &RTC_DateStructure);
    HAL_RTC_SetTime_SetDate(&RTC_InitStructure, &RTC_TimeStructure, RTC_InitStructure.Init.HourFormat, &RTC_DateStructure);
}
/*******************************************************************************
 * Function    : SWDG_Init
 * Brief       : 软件计时初始化
 * Parameter   : 
 * Returns     : 
 * Date        : 2020-05-25 13:46:29
 * Description : 
 ******************************************************************************/
void SWDG_Init(void)
{
    //设定中断时间
    HAL_RTC_Alarm2_Config(&RTC_InitStructure, ENABLE, (HAL_RTCAlarm2TypeDef)HAL_RTC_ALARM2_1S);

    /*Set interrupt priority and turn on interrupt*/
    HAL_NVIC_SetPriority(RTC_IRQn, PRIORITY_LOW);
    HAL_RTC_Alarm_INT_Config(&RTC_InitStructure, RTC_ALARM_2, ENABLE);
    HAL_RTC_Alarm_Clear_Flag(RTC_ALARM_2);
    HAL_NVIC_EnableIRQ(RTC_IRQn);
}
/*******************************************************************************
 * Function    : RTC_Printf
 * Brief       : 打印万年历信息
 * param        {*}
 * return       {*}
 * Description : 
 * Date        : 2021-05-14 17:35:03
 ******************************************************************************/
void RTC_Printf(void)
{
    static u32 delay = 0;
    u8 temp8 = 0;
    u32 temp32 = 0;
    u8 am_pm = 0; //0-24,1-AM,2-PM

    if ((GetSysTickCount() - delay) < 5000) //5s
        return;
    delay = GetSysTickCount();

    HAL_RTC_GetTime_Date(&RTC_InitStructure, &RTC_TimeCurrent, &RTC_DateCurrent);

    Uart_Send_String((u8 *)"\r\nNow Time is: ", 15);
    //年
    temp32 = AHL_Data_Hex2Dec_16bit(RTC_DateCurrent.Year);
    // if (temp32 & 0xff000000)
    // {
    //     temp8 = temp32 >> 24;
    //     Uart_Send_Byte2Ascii(temp8);
    // }
    // if (temp32 & 0x00ff0000)
    // {
    //     temp8 = temp32 >> 16;
    //     Uart_Send_Byte2Ascii(temp8);
    // }
    // if (temp32 & 0x0000ff00)
    {
        temp8 = temp32 >> 8;
        Uart_Send_Byte2Ascii(temp8);
    }
    // if (temp32 & 0x000000ff)
    {
        temp8 = temp32 & 0xff;
        Uart_Send_Byte2Ascii(temp8);
    }
    Uart_Send_Byte('-');

    //月
    temp8 = AHL_Data_Hex2Dec_8bit(RTC_DateCurrent.Month);
    Uart_Send_Byte2Ascii(temp8);
    Uart_Send_Byte('-');

    //日
    temp8 = AHL_Data_Hex2Dec_8bit(RTC_DateCurrent.Date);
    Uart_Send_Byte2Ascii(temp8);
    Uart_Send_Byte(' ');

    //时
    temp8 = RTC_TimeCurrent.Hours;
    if (RTC_InitStructure.Init.HourFormat == RTC_HOURFORMAT_12)
    { //12小时制

        am_pm = 1; //AM
        if (temp8 >= 12)
        {
            temp8 -= 12;
            am_pm = 2; //PM
        }
    }
    else
    { //24小时制

        am_pm = 0; //PM
    }
    temp8 = AHL_Data_Hex2Dec_8bit(temp8);
    Uart_Send_Byte2Ascii(temp8);
    Uart_Send_Byte(':');

    //分
    temp8 = AHL_Data_Hex2Dec_8bit(RTC_TimeCurrent.Minutes);
    Uart_Send_Byte2Ascii(temp8);
    Uart_Send_Byte(':');

    //秒
    temp8 = AHL_Data_Hex2Dec_8bit(RTC_TimeCurrent.Seconds);
    Uart_Send_Byte2Ascii(temp8);

    if (am_pm == 1)
        Uart_Send_String((u8 *)" AM", 3);
    else if (am_pm == 2)
        Uart_Send_String((u8 *)" PM", 3);
}
